package com.luck.emp.kind.web;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.luck.emp.entity.EmpTProdKind;
import com.luck.emp.kind.logic.IKindLogic;
import com.luck.emp.kind.qry.KindQry;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.action.BaseActionSupport;
import com.luck.pf.util.ObjectUtil;

/**
 * 说明:ACTION类
 * Copyright: Copyright (c) 2011
 * 深圳**科技
 * @author 
 * 
 */
public class KindAction extends BaseActionSupport {
		/**
		 * 逻辑处理类
		 */
		IKindLogic kindLogic;
		/**
		 * 查询对象
		 */
		KindQry qry;
		List<EmpTProdKind> kinds;
		Long deptId;
		/**
		 * 实体类
		 */
		EmpTProdKind entity;
		Long parentId;//父ID
		EmpTProdKind parentKind;
		
		public String index() throws Exception{
			return "index";
		}
		public String tree() throws Exception{
			UserBean userBean = super.getUserInfo();
			if (StringUtils.isNotBlank(super.qryHex)) {
				qry = (KindQry)ObjectUtil.getObjectFromHex(qryHex);
			}
			if (qry == null) {
				qry = new KindQry();
			}
			qry.setUserInfo(userBean);
			kinds = kindLogic.findProdKinds(userBean, null);
			super.title="商品分类树";
			return "tree";
		}
		/**
		 * 选择商品分类公用方法
		 * @return
		 * @throws Exception
		 */
		public String select() throws Exception{
			UserBean userBean = super.getUserInfo();
			if (StringUtils.isNotBlank(super.qryHex)) {
				qry = (KindQry)ObjectUtil.getObjectFromHex(qryHex);
			}
			if (qry == null) {
				qry = new KindQry();
			}
			qry.setUserInfo(userBean);
			kinds = kindLogic.findProdKinds(userBean, null);
			super.title="商品分类树";
			return "select";
		}
		/**
		 * 选择商品分类跳转商品方法
		 * @return
		 * @throws Exception
		 */
		public String product() throws Exception{
			UserBean userBean = super.getUserInfo();
			if (StringUtils.isNotBlank(super.qryHex)) {
				qry = (KindQry)ObjectUtil.getObjectFromHex(qryHex);
			}
			if (qry == null) {
				qry = new KindQry();
			}
			qry.setUserInfo(userBean);
			kinds = kindLogic.findProdKinds(userBean, null);
			super.title="商品分类树";
			return "prodKindTree";
		}
		/**
		 * 新增
		 **/
		public String toAdd() throws Exception{
			UserBean userInfo = super.getUserInfo();
			entity = new EmpTProdKind();
			super.event="ADD";
			super.title="新增商品分类";
			parentKind = kindLogic.get(EmpTProdKind.class, parentId);
			entity.setParentId(parentId);
			entity.setParentKind(parentKind);
			return "edit";
		}
		/**
		 * 修改
		 **/
		public String toEdit() throws Exception{
			UserBean userInfo = super.getUserInfo();
			entity = kindLogic.get(EmpTProdKind.class,entity.getId());
			super.event="EDIT";
			super.title="修改商品分类";
			return "edit";
		}
		/**
		 * 修改动作
		 **/
		public String edit() throws Exception{
			UserBean userInfo = super.getUserInfo();
			String str="";
			try {
				if("ADD".equals(super.event)){
					//新增操作
					entity.setId(null);
					entity.setDeptId(userInfo.getDeptId());
					kindLogic.saveKind(userInfo, entity);
					super.addAttribute("qry.pk", entity.getId()+"");
				}else if("EDIT".equals(super.event)){
					//修改操作
					EmpTProdKind db = kindLogic.get(EmpTProdKind.class,entity.getId());
					super.copyProperties(db, entity);
					kindLogic.saveKind(userInfo, db);
					super.addAttribute("qryHex", super.qryHex);
				}
			} catch (Exception e) {
				str = e.getMessage();
			}
			Map<String, String> jsonMap = new HashMap<String, String>();
			if(StringUtils.isBlank(str)){
				jsonMap.put("status", SUCCESS);
				jsonMap.put("kindId", entity.getParentId()+"");
				jsonMap.put("message", "操作成功");
			}else{
				jsonMap.put("status", ERROR);
				jsonMap.put("kindId", entity.getParentId()+"");
				jsonMap.put("message", str);
			}
			JSONObject jsonObject = JSONObject.fromObject(jsonMap);
			this.rendText(jsonObject.toString());
			return null;
		}
		/**
		 * 删除
		 **/
		public String delete() throws Exception{			
			UserBean userInfo = super.getUserInfo();
			String str="";
			try {
				entity = kindLogic.get(EmpTProdKind.class,entity.getId());
				parentId = entity.getParentId();
				str = this.kindLogic.deleteKind(userInfo, entity);
			} catch (Exception e) {
				str = e.getMessage();
			}
			if(StringUtils.isBlank(str)){
				Map<String, String> jsonMap = new HashMap<String, String>();
				jsonMap.put("status", SUCCESS);
				jsonMap.put("kindId", parentId+"");
				jsonMap.put("message", "操作成功");
				JSONObject jsonObject = JSONObject.fromObject(jsonMap);
				this.rendText(jsonObject.toString());
			}else{
				super.renderJsonError(str+"此分类不能删除");
			}
			return null;
		}
		
		public KindQry getQry(){
			return this.qry;
		}
		public void setQry(KindQry qry){
			this.qry = qry;
		}
		
		public IKindLogic getKindLogic(){
			return this.kindLogic;
		}
		public void setKindLogic(IKindLogic kindLogic){
			this.kindLogic = kindLogic;
		}

		public List<EmpTProdKind> getKinds() {
			return kinds;
		}
		public void setKinds(List<EmpTProdKind> kinds) {
			this.kinds = kinds;
		}
		public Long getDeptId() {
			return deptId;
		}
		public void setDeptId(Long deptId) {
			this.deptId = deptId;
		}
		public Long getParentId() {
			return parentId;
		}
		public void setParentId(Long parentId) {
			this.parentId = parentId;
		}
		public EmpTProdKind getParentKind() {
			return parentKind;
		}
		public void setParentKind(EmpTProdKind parentKind) {
			this.parentKind = parentKind;
		}
		public EmpTProdKind getEntity(){
			return entity;
		}
		public void setEntity(EmpTProdKind entity){
			this.entity = entity;
		}
}